Imports cmplib01

Public Class frmAddOOBUnit
    Inherits System.Windows.Forms.Form

    Private m_Cancelled As Boolean = False

    Private ptrCurrentGame As CampaignerGameData
    Private ptrNewUnit As GroundChitType
    '    Private ptrDeploymentZone As DeploymentZone
    Private bunitSelected As Boolean = False
    Private selectedUnitType As UnitType

    Private m_ActionCancelled As Boolean

    Public ReadOnly Property SelectedUnit() As UnitType
        Get
            Return selectedUnitType
        End Get
    End Property


#Region " Windows Form Designer generated code "

    Private Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents pnlFactionOneProperties As System.Windows.Forms.Panel
    Friend WithEvents lblFactionOneDeployedPoints As System.Windows.Forms.Label
    Friend WithEvents txtPointValue As System.Windows.Forms.TextBox
    Friend WithEvents lblChitName As System.Windows.Forms.Label
    Friend WithEvents txtChitName As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents imlUnitIcons As System.Windows.Forms.ImageList
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents txtAvailablePoints As System.Windows.Forms.TextBox
    Friend WithEvents txtSelectedUnitDescription As System.Windows.Forms.TextBox
    Friend WithEvents tvwUnitTypes As System.Windows.Forms.TreeView
    Friend WithEvents lvwElements As System.Windows.Forms.ListView
    Friend WithEvents colCount As System.Windows.Forms.ColumnHeader
    Friend WithEvents colElementName As System.Windows.Forms.ColumnHeader
    Friend WithEvents btnElements As System.Windows.Forms.Button
    Friend WithEvents chkUseElementsForPointValue As System.Windows.Forms.CheckBox
    Friend WithEvents lblSelectUnitType As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmAddOOBUnit))
        Me.btnOK = New System.Windows.Forms.Button
        Me.pnlFactionOneProperties = New System.Windows.Forms.Panel
        Me.lblSelectUnitType = New System.Windows.Forms.Label
        Me.chkUseElementsForPointValue = New System.Windows.Forms.CheckBox
        Me.btnElements = New System.Windows.Forms.Button
        Me.tvwUnitTypes = New System.Windows.Forms.TreeView
        Me.imlUnitIcons = New System.Windows.Forms.ImageList(Me.components)
        Me.Label1 = New System.Windows.Forms.Label
        Me.txtAvailablePoints = New System.Windows.Forms.TextBox
        Me.lblFactionOneDeployedPoints = New System.Windows.Forms.Label
        Me.txtPointValue = New System.Windows.Forms.TextBox
        Me.lblChitName = New System.Windows.Forms.Label
        Me.txtChitName = New System.Windows.Forms.TextBox
        Me.lvwElements = New System.Windows.Forms.ListView
        Me.colCount = New System.Windows.Forms.ColumnHeader
        Me.colElementName = New System.Windows.Forms.ColumnHeader
        Me.txtSelectedUnitDescription = New System.Windows.Forms.TextBox
        Me.btnCancel = New System.Windows.Forms.Button
        Me.pnlFactionOneProperties.SuspendLayout()
        Me.SuspendLayout()
        '
        'btnOK
        '
        Me.btnOK.BackColor = System.Drawing.SystemColors.Control
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.Location = New System.Drawing.Point(312, 360)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(75, 23)
        Me.btnOK.TabIndex = 20
        Me.btnOK.Text = "OK"
        Me.btnOK.UseVisualStyleBackColor = False
        '
        'pnlFactionOneProperties
        '
        Me.pnlFactionOneProperties.BackColor = System.Drawing.SystemColors.Control
        Me.pnlFactionOneProperties.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlFactionOneProperties.Controls.Add(Me.lblSelectUnitType)
        Me.pnlFactionOneProperties.Controls.Add(Me.chkUseElementsForPointValue)
        Me.pnlFactionOneProperties.Controls.Add(Me.btnElements)
        Me.pnlFactionOneProperties.Controls.Add(Me.tvwUnitTypes)
        Me.pnlFactionOneProperties.Controls.Add(Me.Label1)
        Me.pnlFactionOneProperties.Controls.Add(Me.txtAvailablePoints)
        Me.pnlFactionOneProperties.Controls.Add(Me.lblFactionOneDeployedPoints)
        Me.pnlFactionOneProperties.Controls.Add(Me.txtPointValue)
        Me.pnlFactionOneProperties.Controls.Add(Me.lblChitName)
        Me.pnlFactionOneProperties.Controls.Add(Me.txtChitName)
        Me.pnlFactionOneProperties.Controls.Add(Me.lvwElements)
        Me.pnlFactionOneProperties.Controls.Add(Me.txtSelectedUnitDescription)
        Me.pnlFactionOneProperties.Location = New System.Drawing.Point(8, 8)
        Me.pnlFactionOneProperties.Name = "pnlFactionOneProperties"
        Me.pnlFactionOneProperties.Size = New System.Drawing.Size(464, 344)
        Me.pnlFactionOneProperties.TabIndex = 18
        '
        'lblSelectUnitType
        '
        Me.lblSelectUnitType.Location = New System.Drawing.Point(240, 40)
        Me.lblSelectUnitType.Name = "lblSelectUnitType"
        Me.lblSelectUnitType.Size = New System.Drawing.Size(104, 16)
        Me.lblSelectUnitType.TabIndex = 23
        Me.lblSelectUnitType.Text = "Select Unit Type"
        '
        'chkUseElementsForPointValue
        '
        Me.chkUseElementsForPointValue.CheckAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.chkUseElementsForPointValue.Location = New System.Drawing.Point(16, 316)
        Me.chkUseElementsForPointValue.Name = "chkUseElementsForPointValue"
        Me.chkUseElementsForPointValue.Size = New System.Drawing.Size(120, 20)
        Me.chkUseElementsForPointValue.TabIndex = 22
        Me.chkUseElementsForPointValue.Text = "Use Elements PV"
        '
        'btnElements
        '
        Me.btnElements.BackColor = System.Drawing.SystemColors.Control
        Me.btnElements.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnElements.Location = New System.Drawing.Point(152, 312)
        Me.btnElements.Name = "btnElements"
        Me.btnElements.Size = New System.Drawing.Size(75, 23)
        Me.btnElements.TabIndex = 21
        Me.btnElements.Text = "Elements"
        Me.btnElements.UseVisualStyleBackColor = False
        '
        'tvwUnitTypes
        '
        Me.tvwUnitTypes.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.tvwUnitTypes.ImageIndex = 0
        Me.tvwUnitTypes.ImageList = Me.imlUnitIcons
        Me.tvwUnitTypes.Location = New System.Drawing.Point(240, 64)
        Me.tvwUnitTypes.Name = "tvwUnitTypes"
        Me.tvwUnitTypes.SelectedImageIndex = 0
        Me.tvwUnitTypes.ShowLines = False
        Me.tvwUnitTypes.ShowPlusMinus = False
        Me.tvwUnitTypes.ShowRootLines = False
        Me.tvwUnitTypes.Size = New System.Drawing.Size(216, 152)
        Me.tvwUnitTypes.TabIndex = 11
        '
        'imlUnitIcons
        '
        Me.imlUnitIcons.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
        Me.imlUnitIcons.ImageSize = New System.Drawing.Size(16, 16)
        Me.imlUnitIcons.TransparentColor = System.Drawing.Color.Transparent
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(15, 67)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(97, 16)
        Me.Label1.TabIndex = 8
        Me.Label1.Text = "Faction PV"
        '
        'txtAvailablePoints
        '
        Me.txtAvailablePoints.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtAvailablePoints.Location = New System.Drawing.Point(119, 64)
        Me.txtAvailablePoints.Name = "txtAvailablePoints"
        Me.txtAvailablePoints.ReadOnly = True
        Me.txtAvailablePoints.Size = New System.Drawing.Size(112, 21)
        Me.txtAvailablePoints.TabIndex = 7
        Me.txtAvailablePoints.Text = "0"
        Me.txtAvailablePoints.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'lblFactionOneDeployedPoints
        '
        Me.lblFactionOneDeployedPoints.Location = New System.Drawing.Point(16, 96)
        Me.lblFactionOneDeployedPoints.Name = "lblFactionOneDeployedPoints"
        Me.lblFactionOneDeployedPoints.Size = New System.Drawing.Size(97, 16)
        Me.lblFactionOneDeployedPoints.TabIndex = 6
        Me.lblFactionOneDeployedPoints.Text = "Unit Point Value"
        '
        'txtPointValue
        '
        Me.txtPointValue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtPointValue.Location = New System.Drawing.Point(120, 96)
        Me.txtPointValue.Name = "txtPointValue"
        Me.txtPointValue.Size = New System.Drawing.Size(112, 21)
        Me.txtPointValue.TabIndex = 5
        Me.txtPointValue.Text = "0"
        Me.txtPointValue.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'lblChitName
        '
        Me.lblChitName.Location = New System.Drawing.Point(16, 8)
        Me.lblChitName.Name = "lblChitName"
        Me.lblChitName.Size = New System.Drawing.Size(168, 24)
        Me.lblChitName.TabIndex = 3
        Me.lblChitName.Text = "Chit Name"
        '
        'txtChitName
        '
        Me.txtChitName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtChitName.Location = New System.Drawing.Point(16, 32)
        Me.txtChitName.Name = "txtChitName"
        Me.txtChitName.Size = New System.Drawing.Size(216, 21)
        Me.txtChitName.TabIndex = 0
        Me.txtChitName.Text = "Enter a name"
        '
        'lvwElements
        '
        Me.lvwElements.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lvwElements.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.colCount, Me.colElementName})
        Me.lvwElements.GridLines = True
        Me.lvwElements.Location = New System.Drawing.Point(16, 128)
        Me.lvwElements.Name = "lvwElements"
        Me.lvwElements.Size = New System.Drawing.Size(216, 176)
        Me.lvwElements.TabIndex = 12
        Me.lvwElements.UseCompatibleStateImageBehavior = False
        Me.lvwElements.View = System.Windows.Forms.View.Details
        '
        'colCount
        '
        Me.colCount.Text = "Count"
        Me.colCount.Width = 44
        '
        'colElementName
        '
        Me.colElementName.Text = "Element Name"
        Me.colElementName.Width = 152
        '
        'txtSelectedUnitDescription
        '
        Me.txtSelectedUnitDescription.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtSelectedUnitDescription.Location = New System.Drawing.Point(240, 224)
        Me.txtSelectedUnitDescription.Multiline = True
        Me.txtSelectedUnitDescription.Name = "txtSelectedUnitDescription"
        Me.txtSelectedUnitDescription.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtSelectedUnitDescription.Size = New System.Drawing.Size(216, 83)
        Me.txtSelectedUnitDescription.TabIndex = 10
        Me.txtSelectedUnitDescription.Text = "<No Unit Selected>"
        '
        'btnCancel
        '
        Me.btnCancel.BackColor = System.Drawing.SystemColors.Control
        Me.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnCancel.Location = New System.Drawing.Point(392, 360)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(75, 23)
        Me.btnCancel.TabIndex = 21
        Me.btnCancel.Text = "Cancel"
        Me.btnCancel.UseVisualStyleBackColor = False
        '
        'frmAddOOBUnit
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(482, 392)
        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.btnOK)
        Me.Controls.Add(Me.pnlFactionOneProperties)
        Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "frmAddOOBUnit"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Add Unit To Order Of Battle"
        Me.pnlFactionOneProperties.ResumeLayout(False)
        Me.pnlFactionOneProperties.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub New(ByRef GameData As CampaignerGameData, ByRef NewUnit As UnitType)
        Me.New()

        ptrCurrentGame = GameData
        ptrNewUnit = NewUnit

        ' populate the imagelist and units list with the units available
        PopulateUnitsList()

        ' if there are no points remaining for this turn, return false
        ShowAvailablePoints()

    End Sub


    Private Sub PopulateUnitsList()
        'Dim key As String
        Dim unit As UnitType
        Dim index As Int16

        For Each unit In ptrCurrentGame.UnitTypes
            '   unit = ptrCurrentGame.UnitTypes.Item(key)
            ' add the unit's icon to the imagelist
            imlUnitIcons.Images.Add(unit.Icon(cmplib01.IconImageIndex_cgd.Owner))
            Dim nodUnit As New TreeNode()
            nodUnit.Text = unit.TypeName
            nodUnit.Tag = unit.Key
            nodUnit.ImageIndex = index
            nodUnit.SelectedImageIndex = index
            tvwUnitTypes.Nodes.Add(nodUnit)
            index += 1
        Next

    End Sub

    Private Sub tvwUnitTypes_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwUnitTypes.AfterSelect
        Dim selNode As TreeNode = e.Node
        Dim unit As UnitType = ptrCurrentGame.UnitTypes.Item(selNode.Tag)

        txtSelectedUnitDescription.Text = unit.Description

        ' set up the selected unit
        selectedUnitType = unit
        bunitSelected = True

    End Sub

    Private Function ShowAvailablePoints() As Boolean
        Dim fact As cmplib01.Faction
        fact = ptrCurrentGame.selectedFaction
        txtAvailablePoints.Text = fact.FactionFreePoints
        'txtZoneName.Text = ptrDeploymentZone.ZoneName

        Return True

    End Function

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        m_ActionCancelled = False

        If ValidateProperties() = True Then
            ' increase the deployed point value for this faction
            Dim fact As cmplib01.Faction
            fact = ptrCurrentGame.selectedFaction
            'NOTE: we don't increase the deployed points amount. Thats just dumb.
            'fact.FactionDeployedPoints += ptrNewUnit.CurrentPointValue
            Me.Hide()
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        m_ActionCancelled = True
        Me.Hide()
    End Sub

    Private Function ValidateProperties() As Boolean
        ' make sure that an item has been selected
        If bunitSelected = False Then
            MessageBox.Show("You must select a unit type to deploy.", "Select Unit Type", MessageBoxButtons.OK)
            Return False
        End If

        ' heres where it is actually set up
        Try
            'TODO: WATCH: I'm not sure if this will cause a weird error with deployed units (and it did)
            ptrNewUnit.CurrentPointValue = CInt(txtPointValue.Text)
            ptrNewUnit.InitialPointValue = CInt(txtPointValue.Text)
            ptrNewUnit.Name = txtChitName.Text
            Return True
        Catch ex As Exception
            HandleException("frmDeployUnit", ex)
            Return False
        End Try
    End Function

#Region "Properites"
    Public ReadOnly Property Cancelled() As Boolean
        Get
            Return m_Cancelled
        End Get
    End Property
#End Region

    Private Sub btnElements_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnElements.Click
        Dim fEditEles As New frmElementPicker(ptrCurrentGame, ptrNewUnit)
        fEditEles.ShowDialog()
        ShowElements()
        If chkUseElementsForPointValue.Checked = True Then
            CalculatePointValueFromElements()
        End If
    End Sub

    Private Sub ShowElements()
        Dim ele As Element

        lvwElements.Items.Clear()

        For Each ele In ptrNewUnit.ChitElements
            Dim listi As New ListViewItem(ele.Count)
            listi.SubItems.Add(ele.ElementTypeName)
            lvwElements.Items.Add(listi)
        Next

    End Sub

    Private Sub chkUseElementsForPointValue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkUseElementsForPointValue.CheckedChanged
        If chkUseElementsForPointValue.Checked = True Then
            txtPointValue.ReadOnly = True
            CalculatePointValueFromElements()
        Else
            txtPointValue.ReadOnly = False
        End If
    End Sub

    Private Function CalculatePointValueFromElements()
        Dim ele As Element
        Dim eleType As ElementType
        Dim tPV As Integer

        Try
            For Each ele In ptrNewUnit.ChitElements
                tPV += ele.TotalCost
            Next
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

        txtPointValue.Text = tPV

    End Function

End Class
